package com.lwy.qiuchao.safety.service.impl;

import com.lwy.qiuchao.safety.entity.SafetyBoxEntity;
import com.lwy.qiuchao.safety.service.SafetyBoxService;
import com.lwy.qiuchao.safety.vo.CabinetInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lwy.common.utils.PageUtils;
import com.lwy.common.utils.Query;

import com.lwy.qiuchao.safety.dao.SafetyCabinetDao;
import com.lwy.qiuchao.safety.entity.SafetyCabinetEntity;
import com.lwy.qiuchao.safety.service.SafetyCabinetService;


@Service("safetyCabinetService")
public class SafetyCabinetServiceImpl extends ServiceImpl<SafetyCabinetDao, SafetyCabinetEntity> implements SafetyCabinetService {

    @Autowired
    SafetyCabinetDao safetyCabinetDao;

    @Autowired
    SafetyBoxService safetyBoxService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SafetyCabinetEntity> page = this.page(
                new Query<SafetyCabinetEntity>().getPage(params),
                new QueryWrapper<SafetyCabinetEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public CabinetInfoVo getCabinetInfo(Long fieldId, Long scId) {

        CabinetInfoVo cabinetInfoVo = new CabinetInfoVo();

        SafetyCabinetEntity cabinetEntity = safetyCabinetDao.selectOne(new QueryWrapper<SafetyCabinetEntity>().eq("field_id", fieldId).and(obj -> {
            obj.eq("sc_id", scId);
        }));

        if (cabinetEntity.getStatus() != 0){

            List<SafetyBoxEntity> list = safetyBoxService.list(new QueryWrapper<SafetyBoxEntity>().eq("sc_id", scId).and(obj -> {
                obj.eq("field_id", fieldId);
            }));

            cabinetInfoVo.setBoxEntityList(list);
        }

        cabinetInfoVo.setFieldId(fieldId);
        cabinetInfoVo.setScId(scId);
        cabinetInfoVo.setStatus(cabinetEntity.getStatus());
        return cabinetInfoVo;
    }

}